//file for testing speed of reading ascii text input and converting it into
// java. 
import java.util.Scanner;
import java.io.*;

public class ScannerTest {
    static char COMMENT = '#'; 

    public static int readFile1(String name){
	BufferedReader bufread = null;
	int count = 0;
	int tot = 0;
		
	try {
	    bufread = new BufferedReader(new FileReader(name));
	    String inputLine;
	    
	    // read data line by line
	    while ( (inputLine = bufread.readLine()) != null ) {
		
		// trim leading and trailing white spaces
		inputLine.trim();
		
		// skip empty or commented lines
		if (inputLine.length() == 0 || inputLine.charAt(0)==COMMENT)
		    continue;

		// keep track of number of data
		// build up a new context characteristics
		
		// read input items
		Scanner scan = new Scanner(inputLine);
		int bitval = scan.nextInt();
		count++;
		tot += bitval;
		
		// set bucket indexes for each context
		while (scan.hasNextInt()) {
		    // read next context value
		    int ctxval = scan.nextInt();
		    tot += ctxval;
		    count++;
		    if (count%1000==0) System.out.print(".");
		}
		
	    }
	} catch (IOException ex) {
	    System.out.println(ex.getMessage());
	} finally {
	    if (bufread != null) try {bufread.close(); } catch (Exception ex){}
	}
	System.out.println("Total:"+ tot);
	return count;
    }

    // single scanner is more than twice as fast as above bufferedReader.readline followed by scanner. 
    // for some reason, this didn't work correctly on a testfile with negative numbers. 
    public static  int readFile2(String name){
	int count = 0;
	int nonintcount = 0;
	int tot = 0;
	try {
	    Scanner scan = new Scanner(new File(name));

	    scan.useDelimiter("[ ]*");
	    // read data line by line
	    while ( scan.hasNext()) {
		while ( scan.hasNextInt()) {
		    tot += scan.nextInt();
		    
		    count++;
		    if (count%1000==0) System.out.print(".");
		}
		String s = scan.next();
		nonintcount++;
	    }
	}
	
	catch (IOException ex) {
	    System.out.println(ex.getMessage());
	}
	System.out.println("Total:"+ tot);
	System.out.println("nonintcount:"+ nonintcount);
	return count;
    }

    public static  int readFile3(String name){
	ProcessBuilder pb= new ProcessBuilder("txt2bytes", name);

	int count = 0;
	int tot = 0;
	InputStream is;
	byte nextbyte;
	try {
	    Process p = pb.start();
	    is = new BufferedInputStream(p.getInputStream());
	    
	    int next;
	    while ( (next = is.read())!=-1) {
		
		count++;
		if (count%1000==0) System.out.print(".");
		// build up a new context characteristics
		nextbyte = (byte) next;
		//System.out.println(scan.next());
		tot += nextbyte;
	    }
	}
	
	catch (IOException ex) {
	    System.out.println(ex.getMessage());
	}
	System.out.println("Total:"+ tot);
	return count;
    }

    public static  int readFile4(String name){
	ProcessBuilder pb= new ProcessBuilder("txt2bytes", name);

	int count = 0;
	int tot = 0;
	InputStream is;
	byte nextbyte;
	try {
	    Process p = pb.start();
	    is = p.getInputStream();
	    
	    int next;
	    while ( (next = is.read())!=-1) {
		
		count++;
		if (count%1000==0) System.out.print(".");
		// build up a new context characteristics
		nextbyte = (byte) next;
		//System.out.println(scan.next());
		tot += nextbyte;
	    }
	}
	
	catch (IOException ex) {
	    System.out.println(ex.getMessage());
	}
	System.out.println("Total:"+ tot);
	return count;
    }


    public static void main (String[] args) {
	String ccFile = args[0];
	System.out.println("readfile4 returned " + readFile4(ccFile));
	System.out.println("readfile3 returned " + readFile3(ccFile));
	System.out.println("readfile2 returned " + readFile2(ccFile));
      	System.out.println("readfile1 returned " + readFile1(ccFile));
    }

}